package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhousb.deviceManager.mapper.AssetMapper;
import com.zhousb.deviceManager.model.entity.KtAsset;
import com.zhousb.deviceManager.model.enums.AssetStatusEnum;
import com.zhousb.deviceManager.model.so.AssetSO;
import com.zhousb.deviceManager.service.AssetService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.Objects;

import static java.util.stream.Collectors.toMap;

/**
 * @author ZhouShenBin
 * @date 2024/6/27 17:24
 * @description
 */
@Service
public class AssetServiceImpl implements AssetService {

    @Resource
    private AssetMapper assetMapper;

    @Override
    public Object page(Page<KtAsset> page, AssetSO so) {
        LambdaQueryWrapper<KtAsset> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .ne(KtAsset::getAssetStatus, AssetStatusEnum.SCRAP.getCode())
                .eq(StringUtils.hasText(so.getClientName()), KtAsset::getClientName, so.getClientName())
                .eq(!Objects.equals(so.getAssetStatus(), AssetStatusEnum.DEFAULT.getCode()), KtAsset::getAssetStatus, so.getAssetStatus())
                .like(StringUtils.hasText(so.getKeyword()), KtAsset::getAssetName, so.getKeyword());
        Page<KtAsset> data = assetMapper.selectPage(page, queryWrapper);
        return data;
    }

    @Override
    public Object pageScrap(Page<KtAsset> page, AssetSO so) {
        LambdaQueryWrapper<KtAsset> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StringUtils.hasText(so.getClientName()), KtAsset::getClientName, so.getClientName())
                .eq(KtAsset::getAssetStatus, AssetStatusEnum.SCRAP.getCode())
                .like(StringUtils.hasText(so.getKeyword()), KtAsset::getAssetName, so.getKeyword());
        return assetMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Object pageLimit(Page<KtAsset> page, AssetSO so) {
        LambdaQueryWrapper<KtAsset> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .ne(KtAsset::getAssetStatus, AssetStatusEnum.SCRAP.getCode())
                .eq(StringUtils.hasText(so.getClientName()), KtAsset::getClientName, so.getClientName())
                .lt(KtAsset::getLimitTime, LocalDate.now().plusMonths(1))
                .like(StringUtils.hasText(so.getKeyword()), KtAsset::getAssetName, so.getKeyword());
        return assetMapper.selectPage(page, queryWrapper);
    }
}
